﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Lapas.Controls
{
    public partial class DataGridViewExtras : DataGridView
    {
        private List<TextBox> listTextBox = new List<TextBox>();

        public bool isFooterField;
        /// <summary>
        /// Có hiển thị footer tính tổng hay không
        /// </summary>
        public bool IsFooter
        {
            get { return isFooterField; }
            set { isFooterField = value; }
        }
        
        private string colectionSum;
        public string ColectionSum
        {
            get { return colectionSum; }
            set { colectionSum = value; }
        }

        public DataGridViewExtras()
        {
            InitializeComponent();
        }

        public DataGridViewExtras(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            this.AllowUserToAddRows = false;
            this.ScrollBars = ScrollBars.Vertical;                      
        }        

        private void SetTextBox(TextBox textBox)
        {
            textBox.ForeColor = Color.Black;
            textBox.BackColor = Color.LightYellow;
            textBox.Font = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold);
            textBox.TextAlign = HorizontalAlignment.Right;
            textBox.ReadOnly = true;
        }

        private void ResizeTextBox()
        {
            if (!isFooterField) return;
            int x, y, widthCell = 0, index = 0;
            if (this.RowHeadersVisible)
            {
                if (listTextBox.Count > 0)
                {
                    listTextBox[index].Location = new Point(this.GetCellDisplayRectangle(-1, -1, true).Location.X, this.Height - listTextBox[0].Height);
                    listTextBox[index].Width = this.RowHeadersWidth;
                    index++;
                }
            }
            for (int i = 0; i < this.Columns.Count; i++)
            {
                if (listTextBox.Count > 0)
                {
                    x = this.GetCellDisplayRectangle(0, -1, true).Location.X + widthCell;
                    y = this.Height - listTextBox[i + index].Height;
                    widthCell += this.Columns[i].Width;
                    listTextBox[i + index].Location = new Point(x, y);
                    listTextBox[i + index].Width = this.Columns[i].Width;                    
                }
            }
            this.Refresh();
        }

        protected override void OnDataSourceChanged(EventArgs e)
        {
            if (Controls.Count == 2 && isFooterField)
            {
                this.listTextBox = new List<TextBox>();
                this.Controls.Clear();                
                if (this.RowHeadersVisible)
                {
                    TextBox box = new TextBox();
                    box.Text = "Tổng";
                    SetTextBox(box);
                    box.TextAlign = HorizontalAlignment.Center;
                    this.Controls.Add(box);
                    listTextBox.Add(box);
                }
                foreach (DataGridViewColumn col in this.Columns)
                {
                    TextBox textBox = new TextBox();
                    SetTextBox(textBox);
                    this.Controls.Add(textBox);
                    listTextBox.Add(textBox);
                }               
            }
            base.OnDataSourceChanged(e);
        }

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);            
            if (!isFooterField) return;            
            decimal total = -1;
            int x, y, index = 0;
            int widthCell = 0;
            int totalHeidht = 0;
            string[] colNames = new string[] { };
            List<string> colSums = new List<string>();
            if (!string.IsNullOrEmpty(ColectionSum))
            {
                colNames = colectionSum.Split(';');
            }
            for (int count = 0; count < colNames.Length; count++)
                colSums.Add(colNames[count]);
            if (this.RowHeadersVisible)
            {
                if (listTextBox.Count > 0)
                {
                    listTextBox[index].Location = new Point(this.GetCellDisplayRectangle(-1, -1, true).Location.X, this.Height - listTextBox[0].Height);
                    listTextBox[index].Width = this.RowHeadersWidth;
                    index++;
                }
            }
            for (int i = 0; i < this.Columns.Count; i++)
            {
                total = -1;
                if (this.Columns[i].DefaultCellStyle.Format.Contains("n") || this.Columns[i].DefaultCellStyle.Format.Contains("c"))
                {                   
                    if (!colSums.Contains(this.Columns[i].Name))
                    {
                        total = 0;
                        for (int j = 0; j < this.Rows.Count; j++)
                        {
                            total += Convert.ToDecimal(this[i, j].Value);
                            totalHeidht += this[i, j].Size.Height;
                        }
                    }
                }
                if (listTextBox.Count > 0)
                {
                    listTextBox[i + index].Text = total == -1 ? string.Empty : total.ToString(this.Columns[i].DefaultCellStyle.Format);
                    x = this.GetCellDisplayRectangle(0, -1, true).Location.X + widthCell;
                    y = this.Height - listTextBox[i + index].Height;
                    widthCell += this.Columns[i].Width;
                    listTextBox[i + index].Location = new Point(x, y);
                    listTextBox[i + index].Width = this.Columns[i].Width;
                }
            }            
        }

        protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
        {
            base.OnColumnWidthChanged(e);
            ResizeTextBox();
        }

        protected override void OnRowHeightChanged(DataGridViewRowEventArgs e)
        {
            base.OnRowHeightChanged(e);
            ResizeTextBox();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            ResizeTextBox();
        }
    }
}
